﻿<?php

/**
 * 
 * Conrtoller for checking the history of a group.
 * @author Théo Chamley
 *
 */
class HistoriqueController extends CodeBar_AbstractController {
	
	public function init() {
		CodeBar_AbstractController::init();
		if(!($this->session()->user->connected)) {
			$this->_helper->redirector('index');
		} else if($this->session()->permissionactive != "Historique") {
			$this->_helper->redirector('PermissionChoice','index');
		}
	}
	
	/**
	 * (non-PHPdoc)
	 * This controller has only one action to see the history.
	 * @see trunk/library/CodeBar/CodeBar_AbstractController::indexAction()
	 */
	public function indexAction() {
		$this->view->headTitle("Historique");
		$this->jQueryUIEnable();
		/*
		 * CSS
		 */
		$this->view->headLink()->appendStylesheet('/css/stylesheet_historique.css');
		$this->view->headLink()->appendStylesheet('/css/histo_table.css');
		$this->view->headLink()->appendStylesheet('/css/TableTools.css');
		
		/*
		 * JS
		 */
		$this->view->headScript()->appendFile('/js/jquery.ui.datepicker-fr.js','text/javascript');
		$this->view->headScript()->appendFile('/js/jquery.dataTables.min.js','text/javascript');
		$this->view->headScript()->appendFile('/js/ZeroClipboard.js','text/javascript');
		$this->view->headScript()->appendFile('/js/TableTools.min.js','text/javascript');
		$this->view->headScript()->appendFile('/js/historique.js','text/javascript');
		$this->view->headScript()->appendFile('/js/Escape.js','text/javascript');
		$this->view->headScript()->appendFile('/js/jquery.dataTables.dateSorting.js','text/javascript');
		
		
		$form = new Application_Form_Historique_HistoFilterForm();
		$this->view->form = $form;
		$histo = array();
		if($this->getRequest()->isPost()) {
			 
			$filtres = array();
			
			$filtres['groupe'] = $this->session()->user->getGroupeActif();
			$filtres['vendeur'] = $this->getRequest()->getPost('vendeur');
			 
			if($this->getRequest()->getPost('datedebut')) {
				$filtres['debut'] = DateTime::createFromFormat('d/m/Y', $this->getRequest()->getPost('datedebut'));
			}
			if($this->getRequest()->getPost('datefin')) {
				$filtres['fin'] = DateTime::createFromFormat('d/m/Y', $this->getRequest()->getPost('datefin'));
			}
			$types = $this->getRequest()->getPost('types');
			
			if(isset($types)&&in_array('esp',$types)) {
				$filtres['esp'] = true;
			} else {
				$filtres['esp'] = false;
			}
			if(isset($types)&&in_array('CB',$types)) {
				$filtres['CB'] = true;
			} else {
				$filtres['CB'] = false;
			}
			$histo = Application_Model_HistoItem::getVFiltre($filtres);
			$this->view->histo = $histo;
		} else {
			/*
			 * At the beginning we don't display any historic as they are to big to be processed.
			 */
			$this->view->histo = array();
		}
		$this->view->sommeCB = Application_Model_HistoItem::somme(Application_Model_HistoItem::codebar($histo));
		$this->view->sommeEsp = Application_Model_HistoItem::somme(Application_Model_HistoItem::especes($histo));
		$this->view->sommeTot = Application_Model_HistoItem::somme($histo);
	}
}